package com.yunqiao.common.utils;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjectUtil;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import com.yunqiao.common.consts.AppConsts;
import com.yunqiao.common.exception.CustomApiException;

import java.util.ArrayList;
import java.util.List;

/**
 * 个推发送消息
 */
public class GeTuiUtils {

    //司机 端配置参数

    private static String appId_driver = "dAg0tBx4iVATYH7bIcMGS9";
    private static String appKey_driver = "4vk9ylw5NS8ZUTeoghnrg6";
    /**
     * 短信key
     */
    private static String masterSecret_driver = "JbY5yipT6r8vZamZHcCAX9";
/**=================================================================================*/
    //用户端配置参数

    private static String appId_user = "Y3vQSL0FL59gSIu9ddn4i";
    private static String appKey_user = "gmCGKkBTBI61QRzHM4Lwb1";
    /**
     * 短信key
     */
    private static String masterSecret_user = "qACudDgye79r5WAUt6bpA4";
    private static String appSecret_user = "hHbV06fS6q8LPy8pnFYAK3";
    /**
     * 个推请求网关
     */
    private static String host = "http://sdk.open.api.igexin.com/apiex.htm";

    /**
     * 推送
     * @param type  1:用户端     2：司机端
     * @param title 通知栏标题
     * @param text 通知栏类容
     * @param content  内容
     * @param clientIds  用户的个推客户端id
     */
    public static void send(Integer type, String title, String text, String content, List<String> clientIds) {
        if (ObjectUtil.notEqual(type, AppConsts.USER_ROLE_TYPE_1) && ObjectUtil.notEqual(type, AppConsts.USER_ROLE_TYPE_2)) {
            throw new CustomApiException("请传入正确的发送端");
        }
        String appkey= ObjectUtil.equal(type, AppConsts.USER_ROLE_TYPE_1) ? appKey_user :appKey_driver;
        String masterSecret=ObjectUtil.equal(type, AppConsts.USER_ROLE_TYPE_1) ? masterSecret_user :masterSecret_driver;
        String appId=ObjectUtil.equal(type, AppConsts.USER_ROLE_TYPE_1) ? appId_user : appId_driver;
        IGtPush push = new IGtPush(host, appkey, masterSecret);
        // 通知透传模板
        NotificationTemplate template = notificationTemplateDemo(title, text, content, type);
        ListMessage message = new ListMessage();
        message.setData(template);
        // 设置消息离线，并设置离线时间
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 1000 * 3600);
        // 配置推送目标
        List targets = new ArrayList();
        if (clientIds != null && clientIds.size() > 0) {
            for (String clientId : clientIds) {
                Target target = new Target();
                target.setAppId(appId);
                target.setClientId(clientId);
                targets.add(target);
            }
        }
        // taskId用于在推送时去查找对应的message
        String taskId = push.getContentId(message);
        IPushResult ret = push.pushMessageToList(taskId, targets);
        Console.log(ret.getResponse());
    }

    /**
     * 发送模板
     * @param type  1:用户端     2：司机端
     * @param title 通知栏标题
     * @param text 通知栏类容
     * @param content  内容
     * @return
     */
    public static NotificationTemplate notificationTemplateDemo(String title, String text, String content, Integer type) {
        if (ObjectUtil.notEqual(type, AppConsts.USER_ROLE_TYPE_1) && ObjectUtil.notEqual(type, AppConsts.USER_ROLE_TYPE_2)) {
            throw new CustomApiException("请传入正确的发送端");
        }
        String appkey= ObjectUtil.equal(type, AppConsts.USER_ROLE_TYPE_1) ? appKey_user :appKey_driver;
        String appId=ObjectUtil.equal(type, AppConsts.USER_ROLE_TYPE_1) ? appId_user : appId_driver;
        NotificationTemplate template = new NotificationTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appkey);
        template.setChannelLevel(4);
        template.setTransmissionType(1);

        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle(title);
        style.setText(text);
        // 配置通知栏图标
        style.setLogo("icon.png");
        // 配置通知栏网络图标
        style.setLogoUrl("");
        // 设置通知是否响铃，震动，或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        template.setStyle(style);

        // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
        template.setTransmissionType(2);
        template.setTransmissionContent(content);
        return template;
    }
}
